window.onload=LoadPage;
window.onresize=ResizeWindow;
window.onbeforeprint = set_to_print;
window.onafterprint = reset_form;

var scrollPos = 0;

function LoadPage()
{
	if(IsEvilUrl())
	{
		ReloadEvilUrlAsGoodUrl();
		return;	// in just a moment, IE will reload the exact same page disguised
				// as a different "good" url
	}
		
	// show correct language
	var langIndex = Load("language");
	if(langIndex == null)
		selectLanguage.selectedIndex = 1;
	else
		selectLanguage.selectedIndex = langIndex;
	ChangeLanguage();

	// show correct style sheet
	var styleIndex = Load("style");
	if(styleIndex == null)
		selectStyleSheet.selectedIndex = 0;
	else
		selectStyleSheet.selectedIndex = styleIndex;
	ChangeStyleSheet();

	ResizeWindow();

	// vs70.js did this to allow up/down arrow scrolling, I think
	try { mainSection.setActive(); } catch(e) { }

	// make body visible, now that we're ready to render
	document.body.style.display = "";
	
	//set the scroll position
	try{mainSection.scrollTop = scrollPos;}
	catch(e){}
}

function ResizeWindow()
{
	if (document.body.clientWidth==0) return;
	var header = document.all.item("header");
	var mainSection = document.all.item("mainSection");
	if (mainSection == null) return;
	
	
	document.body.scroll = "no"
	mainSection.style.overflow= "auto";
	header.style.width= document.body.offsetWidth - 2;
	mainSection.style.paddingRight = "20px"; // Width issue code
	mainSection.style.width= document.body.offsetWidth - 4;
	mainSection.style.top=0;  
	if (document.body.offsetHeight > header.offsetHeight + 10)
		mainSection.style.height= document.body.offsetHeight - (header.offsetHeight + 10) 
	else
		mainSection.style.height=0
	
	try
	{
		mainSection.setActive();
	}
	catch(e)
	{
	}
}

function ChangeLanguage()
{
	var langIndex = selectLanguage.selectedIndex;
	var lang = selectLanguage.options[selectLanguage.selectedIndex].value;

	// update each style sheet
	for(j = 0; j < document.styleSheets.length; ++j)
	{
		var currentRules = document.styleSheets[j].rules;
		for(i = 0; i < currentRules.length; ++i)
		{			
			if(currentRules[i].selectorText.substring(0, 6) == ".code_")
			{
				if(currentRules[i].selectorText == ".code_" + lang || lang == "(all)")
				{
					currentRules[i].style.display = "block";
				}
				else
				{
					currentRules[i].style.display = "none";
				}
			}
		}
	}
	Save("language", langIndex);
}

function ChangeSeeAlso()
{
	var url = selectSeeAlso.options[selectSeeAlso.selectedIndex].value;
	location.href = url;
}

function ChangeStyleSheet()
{
	var styleIndex = selectStyleSheet.selectedIndex;
	
	for(i = 0; i < document.styleSheets.length; ++i)
	{
		if(i == styleIndex)
			document.styleSheets[i].disabled = false;
		else
			document.styleSheets[i].disabled = true;
	}
	HxLinks.disabled = false;
	Save("style", styleIndex);
}

function IsEvilUrl()
{
	var url = "" + document.location + ".";
	var r = url.indexOf("mk:@MSITStore") != -1;
	return r;
}

function IsGoodUrl()
{
	return !IsEvilUrl();
}

function ReloadEvilUrlAsGoodUrl()
{
	var url = "" + document.location + ".";
	var i = url.indexOf("mk:@MSITStore");
	if(i != -1)
	{
		url = "ms-its:" + url.substring(14, url.length - 1);
		document.location.replace(url);
	}
}

function Load(key)
{
	if(IsGoodUrl())
	{
		userDataCache.load("docSettings");
		var value = userDataCache.getAttribute(key);
		return value;
	}
}

function Save(key, value)
{
	if(IsGoodUrl())
	{
		userDataCache.setAttribute(key, value);
		userDataCache.save("docSettings");
	}
}

function loadAll(){
	try 
	{
		scrollPos = allHistory.getAttribute("Scroll");
	}
	catch(e){}
}

function saveAll(){
	try
	{
		allHistory.setAttribute("Scroll", mainSection.scrollTop);
	}
	catch(e){}
}

function set_to_print()
{
	//breaks out of divs to print

	var i;

	if (window.text)document.all.text.style.height = "auto";
			
	for (i=0; i < document.all.length; i++)
	{
		if (document.all[i].tagName == "body") 
		{
			document.all[i].scroll = "yes";
		}
		if (document.all[i].id == "header") 
		{
			document.all[i].style.margin = "0px 0px 0px 0px";
			document.all[i].style.width = "100%";
		}
		if (document.all[i].id == "mainSection") 
		{
			document.all[i].style.overflow = "visible";
			document.all[i].style.top = "5px";
			document.all[i].style.width = "100%";
			document.all[i].style.padding = "0px 10px 0px 30px";
		}
	}
}


function reset_form()
{
	//returns to the div nonscrolling region after print
	 document.location.reload();
}
